/*
* Copyright 2005-2009 Jesse Andrews
* Copyright 2011 Stepan Riha
*
* This file may be used under the terms of of the
* GNU General Public License Version 3 or later (the "GPL"),
* http://www.gnu.org/licenses/gpl.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/

/****************
 * STORE/LIBRARY site wapper
 */

function Site(data) {
	if (!data) return;
	this._data = data;
	this.title = data.title;
	this.name  = data.name;
	this.id = this.site_id();
}

Site.prototype.url = function site_url(isbn, affiliate) {
	affiliate = false;
	
	if (affiliate && this._data.affiliate_link) {
		var link = this._data.affiliate_link;
	} else {
		var link = this._data.link;
	}
	
	return link.replace('#{ISBN}', isbn.toLowerCase())
			.replace('#{ISBN_UPCASE}', isbn.toUpperCase());
};

Site.prototype.query_url = function site_url(isbn) {
	if(!this._data.query) {
		return this.url(isbn, false);
	}

	var link = this._data.query;
	
	return link.replace('#{ISBN}', isbn.toLowerCase())
			.replace('#{ISBN_UPCASE}', isbn.toUpperCase());
};

Site.prototype.active = function site_active() {
	return Prefs.getValue(this.id, false);
};

Site.prototype.site_id = function site_id() {
	return this.kind + '-' + this.name.replace(/\./g, '-');
}

function Store(data) {
	this.kind  = 'book_stores';
	Site.call(this, data);
}

Store.prototype = new Site;
Store.prototype.constructor = Store;

Store.prototype.process = function store_process(req, isbn) {
	try {
		if (this._data.match) {
			var results = req.responseText.match(this._data.match);
			if (results && results.length > 1) {
				var status = results[1].replace(/^\s*/, '').replace(/\s*$/, '');
				if (status[0] != '$') status = '$' + status;
				if (status.indexOf('.') == -1 ) status = status + '.00';
				if (status.indexOf('.') + 2 == status.length ) status = status + '0';
				return status;
			} else {
				return "no";
			}
		}		
		return this._data.process(req, isbn);
	} catch (e) {
		return '!'; // XXX - this causes the exception to be cached
	}
};

function Library(data){
	if (!data) return;
	this.kind  = this.kind || 'libraries';
	Site.call(this, data);
	this.have = data.have;
	this.dont = data.dont;
	this.link = data.link;
	this.missing = data.missing;
	this.match_isbn = data.match_isbn;
	this.is_community = data.is_community;
}

Library.prototype = new Site;
Library.prototype.constructor = Library;

Library.prototype.process = function library_process(req, isbn) {
	var matched = false;
	if (this._data.match_isbn) {
		var results = req.responseText.toLowerCase().match( isbn.toLowerCase() );
		matched = results && results.length > 0;
	}
	
	if (this._data.found) {
		var regex = new RegExp(this._data.found);
		var results = req.responseText.match( regex );
		matched = results && results.length > 0;
	}
	
	if (this._data.missing) {
		var regex = new RegExp(this._data.missing);
		var results = req.responseText.match( regex );
		matched = !results || results.length < 0;
	}
	
	 return matched ? "yes" : "no";
	 
	// XXX - throw an error
};

CustomLibrary.prototype = new Library;
CustomLibrary.prototype.constructor = CustomLibrary;

function CustomLibrary(data){
	this.kind  = 'custom';
	Library.call(this, data);
}
